<template>
	<view class="container">
		<!-- 顶部筛选区域 -->
		<view class="header-section">
			<view class="filter-row">
				<view class="f f-a-c">
					<text class="selector-text">班级:</text>
					<view class="class-selector" @tap="showClassFilter">
						<text class="selector-texts">{{ selectedClass }}</text>
						<text class="dropdown-icon">▼</text>
					</view>

				</view>
				<text class="total-count">总人数: {{total}}人</text>
			</view>

			<!-- 搜索栏 -->
			<view class="search-row">
				<view class="search-container">
					<image src="/static/class/sech.png" class="search-icon" mode="aspectFit"></image>
					<input v-model="searchKeyword" class="search-input" placeholder="搜索学生" @input="onSearchInput"
						@confirm="onSearchConfirm" />
				</view>
				<view class="add-btn" @tap="addStudent">
					<!-- <text class="add-icon">+</text> -->
					<image style="width: 100%;height: 100%;" src="/static/class/jia.png" mode=""></image>
				</view>
			</view>
		</view>

		<!-- 学生列表 -->
		<scroll-view class="student-list" scroll-y="true" :refresher-triggered="isRefreshing"
			@refresherrefresh="onRefresh" @scrolltolower="bottomScool" @refresherrestore="onRefreshRestore">
			<view class="list-content">
				<view v-for="(item, index) in studentList" :key="item.id" class="student-item"
					@tap="viewStudentDetail(item)">
					<u-swipe-action :index="index" @click="delet(item)" @open="open" :options="options">
						<view class="student-content">
							<view class="student-info">
								<view class="student-header">
									<text class="student-name">{{item.studentName}}</text>
									<text class="student-class">{{selectedClass}}</text>
								</view>
								<text class="student-id">学号:{{item.studentNo}}</text>
							</view>
							<!-- <view class="action-area f f-a-c " style="justify-content:end;">
								<view class="checkin-btn f f-a-c f-j-c">查看</view>
							</view> -->
						</view>
					</u-swipe-action>
				</view>

				<!-- 空状态 -->
				<view v-if="!studentList.length" class="empty-state">
					<!-- <image src="/static/icons/empty.png" class="empty-icon" mode="aspectFit"></image> -->
					<text class="empty-text">暂无学生数据</text>
				</view>
			</view>
		</scroll-view>

		<u-select v-model="show" value-name="classId" label-name="className" @confirm="confirm" :list="list"></u-select>
		<u-popup mode="center" border-radius="20" v-model="showPop">
			<view class="popBox">
				<view class="popTop">
					<view class="addStud f f-a-c f-j-c">
						添加学生
					</view>
				</view>
				<view class="nameBox f f-a-c f-j-b">
					<view class="name">
						学生姓名:
					</view>
					<input type="text" class="inp" v-model="form.name" placeholder="请输入" />
				</view>
				<view class="nameBox f f-a-c f-j-b">
					<view class="name">
						学生学号:
					</view>

					<input type="text" class="inp" v-model="form.code" placeholder="请输入" />
				</view>
				<view class="f f-a-c f-j-c">
					<view @click="addStudet" class="comfig f f-a-c f-j-c">
						确认
					</view>
				</view>
				<image src="/static/class/x.png" @click="xDiao" class="x" mode=""></image>
			</view>
		</u-popup>
		<u-modal v-model="showQiang" :show-cancel-button="true" @confirm="confirmAdd" :content="content"></u-modal>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				disabled: false,
				btnWidth: 180,
				total: 0,
				options: [{
					text: "删除",
					style: {
						backgroundColor: "#dd524d",
					},
				}, ],
				content: '已绑定其他班级，是否强制添加',
				showQiang: false,
				form: {
					name: '',
					code: ''
				},
				showPop: false,
				selectedClass: '',
				selectedID: '0', //选择的班级ID
				searchKeyword: '',
				showFilter: false,
				isRefreshing: false,
				isLoading: false,
				hasMore: true,
				currentPage: 1,
				pageSize: 20,

				show: false,
				list: [],

				// 学生列表
				studentList: [{
						id: 1,
						name: '小明',
						class: '3年2班',
						studentId: '00001212',
						isCheckedIn: false
					},

				]
			}
		},

		computed: {


		},
		watch: {
			showPop(newl, oldl) {
				if (!newl) {
					this.form = {
						name: '',
						code: ''
					}
				}
			}
		},
		onShow() {
			this.init()
		},
		methods: {
			open(e) {
				console.log(e, '111');
			},
			// 删除学生
			async delet(item) {
				console.log(item, '4444');
				let n = await this.$api.quitStudent({
					classId: this.selectedID, // 使用当前选中的班级ID
					studentId: item.studentId || item.id // 使用学生的ID
				})
				if (n.code == '0000') {
					this.$msg('删除成功')
					setTimeout(() => {
						this.initStudentList()
					}, 1000)
				}
			},
			async addStudet() {
				let n = await this.$api.joinStudent({
					"schoolId": uni.getStorageSync('schoolId'),
					"studentName": this.form.name,
					"studentNo": this.form.code,
					"classId": this.selectedID,
					"isForceAdd": false
				})
				if (n.code == 'ERR_BIZ_006') {
					// 在某个状态码下要求强制添加，则打开询问框
					this.showQiang = true
				} else if (n.code == '0000') {
					this.$msg('添加成功')
					this.showPop = false
					setTimeout(() => {
						this.initStudentList()
					}, 1000)
				} else {
					this.$msg('添加失败')
				}
			},
			// 点击强制添加
			async confirmAdd() {
				let n = await this.$api.joinStudent({
					"schoolId": uni.getStorageSync('schoolId'),
					"studentName": this.form.name,
					"studentNo": this.form.code,
					"classId": this.selectedID,
					"isForceAdd": true
				})
				if (n.code == '0000') {
					this.$msg('添加成功')
					this.showPop = false
					setTimeout(() => {
						this.initStudentList()
					}, 1000)
				}
			},
			async init() {
				// 获取班级信息
				let classList = await this.$api.classList({
					teacherId: uni.getStorageSync('teacherId')
				})


				// 更新本地的班级列表
				this.list = classList.data.map(item => ({
					...item
				}));
				if (this.list.length) {
					this.selectedClass = this.list[0].className
					this.selectedID = this.list[0].classId

					this.initStudentList()
				}

			},
			// 班级下学生信息
			async initStudentList() {
				// 获取班级下的学生信息
				let studentList = await this.$api.classStudentList({
					classId: this.selectedID,
					studentName: ''
				})

				// 处理学生列表数据中的大数字字段
				if (studentList && studentList.data) {
					this.studentList = studentList.data;
					this.total = studentList.data.length
				}
			},
			// 点击弹出层x
			xDiao() {
				this.showPop = false;
				this.form = {
					name: '',
					code: ''
				}
			},
			confirm(e) {
				console.log(e);
				this.selectedClass = e[0].label
				this.selectedID = e[0].value
				this.initStudentList()
			},
			// 显示班级筛选
			showClassFilter() {
				this.show = true;
			},
			// 搜索输入
			onSearchInput(e) {
				this.searchKeyword = e.detail.value;
			},

			// 搜索确认
			onSearchConfirm() {
				console.log('搜索:', this.searchKeyword);
			},



			// 刷新恢复
			onRefreshRestore() {
				this.isRefreshing = false;
			}, // 刷新恢复
			bottomScool() {
				console.log('11111111');
			},





			// 签到
			checkIn(student, index) {
				if (student.isCheckedIn) {
					uni.showToast({
						title: '该学生已签到',
						icon: 'none'
					});
					return;
				}

				uni.showModal({
					title: '确认签到',
					content: `确定为${student.name}签到吗？`,
					success: (res) => {
						if (res.confirm) {
							student.isCheckedIn = true;
							uni.showToast({
								title: '签到成功',
								icon: 'success'
							});
						}
					}
				});
			},

			// 查看学生详情
			viewStudentDetail(student) {
				uni.navigateTo({
					url: `/pages/class/subpages/viewStudent`
				});
			},

			// 添加学生
			addStudent() {
				this.showPop = true
				// uni.navigateTo({
				// 	url: '/pages/student-add/index'
				// });
			}
		},


	}
</script>

<style lang="scss" scoped>
	// ::v-deep .u-swipe-view {
	// 	width: 100% !important;
	// }

	.comfig {
		width: 492rpx;
		height: 84rpx;
		background: #05C490;
		border-radius: 30rpx;
		font-family: SourceHanSansCN, SourceHanSansCN;
		font-weight: 500;
		font-size: 36rpx;
		color: #FFFFFF;
		margin-top: 50rpx;
	}

	.popBox {
		position: relative;
		height: 476rpx;
		width: 650rpx;
		padding: 25rpx;

		.x {
			position: absolute;
			width: 44rpx;
			height: 44rpx;
			top: 15rpx;
			right: 15rpx;
		}

		.nameBox {
			margin-top: 30rpx;

			.name {
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 400;
				font-size: 34rpx;
				// margin-right: 20rpx;
				color: #333333;
			}

			.inp {
				border-radius: 20rpx;
				border: 2rpx solid #CCCCCC;
				color: #000;
				padding-left: 20rpx;
				height: 70rpx;
				width: 70%;
			}

			.inp::placeholder {
				color: #CCCCCC;

			}
		}

		.popTop {
			.addStud {
				font-family: SourceHanSansCN, SourceHanSansCN;
				font-weight: 600;
				font-size: 32rpx;
				color: #000000;
			}
		}
	}


	.container {
		height: 100vh;
		background-color: #fff;
	}

	.header-section {
		height: 200rpx;
		background-color: white;
		padding: 20rpx 30rpx;
		border-bottom: 1rpx solid #eee;
	}

	.filter-row {
		display: flex;
		justify-content: space-between;
		align-items: center;
		margin-bottom: 20rpx;
	}

	.class-selector {
		display: flex;
		align-items: center;
		padding: 12rpx 20rpx;
		border: 2rpx solid #ddd;
		border-radius: 8rpx;
		background-color: white;
	}

	.selector-text {
		font-size: 32rpx;
		color: #000;
		font-weight: 600;
		margin-right: 10rpx;
	}

	.selector-texts {
		font-size: 32rpx;
		color: #000;
		font-weight: 400;
		margin-right: 10rpx;
	}

	.dropdown-icon {
		font-size: 20rpx;
		color: #666;
	}

	.total-count {
		font-size: 32rpx;
		font-weight: 600;
		color: #000;

	}

	.search-row {
		display: flex;
		align-items: center;
		gap: 20rpx;
	}

	.search-container {
		flex: 1;
		display: flex;
		align-items: center;
		background-color: #f8f9fa;
		border-radius: 25rpx;
		padding: 0 20rpx;
		height: 70rpx;
	}

	.search-icon {
		width: 32rpx;
		height: 32rpx;
		margin-right: 15rpx;
	}

	.search-input {
		flex: 1;
		font-size: 28rpx;
		color: #333;
		background-color: transparent;
		border: none;
		outline: none;
	}

	.add-btn {
		width: 52rpx;
		height: 52rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.add-icon {
		color: white;
		font-size: 36rpx;
		font-weight: 300;
	}

	.student-list {
		flex: 1;
		height: calc(100vh - 200rpx);
	}

	.list-content {
		padding: 20rpx 30rpx;
	}

	.student-item {
		background-color: white;
		border-radius: 12rpx;
		padding: 0;
		margin-bottom: 20rpx;
		box-shadow: 0 2rpx 8rpx rgba(0, 0, 0, 0.05);

		:deep(.u-swipe-action) {
			height: auto !important;
			min-height: auto !important;
		}

		:deep(.u-swipe-action__content) {
			display: flex !important;
			justify-content: space-between !important;
			align-items: center !important;
			height: auto !important;
			min-height: auto !important;
		}
	}

	.student-content {
		display: flex;
		justify-content: space-between;
		align-items: center;
		width: 650rpx;
		flex: 1;
		padding: 30rpx;
	}

	.student-info {
		flex: 1;
	}

	.student-header {
		display: flex;
		align-items: center;
		margin-bottom: 10rpx;
	}

	.student-name {
		font-size: 32rpx;
		font-weight: 500;
		color: #333;
		margin-right: 20rpx;
	}

	.student-class {
		font-size: 24rpx;
		color: #8e44ad;
		background-color: #f8f9fa;
		padding: 4rpx 12rpx;
		border-radius: 12rpx;
	}

	.student-id {
		font-size: 26rpx;
		color: #666;
	}

	.action-area {
		margin-left: 20rpx;
	}

	.checkin-btn {
		width: 92rpx;
		height: 40rpx;
		background: #05C490;
		border-radius: 10rpx;
		font-family: SourceHanSansCN, SourceHanSansCN;
		font-weight: 400;
		font-size: 22rpx;
		color: #FFFFFF;


	}

	.checkin-btn::after {
		border: none;
	}

	.empty-state {
		display: flex;
		flex-direction: column;
		align-items: center;
		justify-content: center;
		padding: 100rpx 0;
	}

	.empty-icon {
		width: 120rpx;
		height: 120rpx;
		margin-bottom: 20rpx;
		opacity: 0.5;
	}

	.empty-text {
		font-size: 28rpx;
		color: #999;
	}

	.filter-overlay {
		position: fixed;
		top: 0;
		left: 0;
		right: 0;
		bottom: 0;
		background-color: rgba(0, 0, 0, 0.5);
		display: flex;
		align-items: flex-end;
		z-index: 1000;
	}

	.filter-popup {
		background-color: white;
		border-radius: 20rpx 20rpx 0 0;
		width: 100%;
		max-height: 80vh;
		animation: slideUp 0.3s ease;
	}

	@keyframes slideUp {
		from {
			transform: translateY(100%);
		}

		to {
			transform: translateY(0);
		}
	}

	.filter-header {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 30rpx;
		border-bottom: 1rpx solid #eee;
	}

	.filter-title {
		font-size: 32rpx;
		font-weight: bold;
		color: #333;
	}

	.close-btn {
		width: 60rpx;
		height: 60rpx;
		display: flex;
		align-items: center;
		justify-content: center;
	}

	.close-icon {
		font-size: 40rpx;
		color: #999;
	}

	.filter-content {
		max-height: 400rpx;
		overflow-y: auto;
	}

	.filter-item {
		display: flex;
		justify-content: space-between;
		align-items: center;
		padding: 25rpx 30rpx;
		border-bottom: 1rpx solid #f0f0f0;

		&.selected {
			background-color: #f8f9fa;
		}
	}

	.filter-item-text {
		font-size: 28rpx;
		color: #333;
	}

	.check-icon {
		font-size: 28rpx;
		color: #007bff;
		font-weight: bold;
	}

	.filter-footer {
		display: flex;
		gap: 20rpx;
		padding: 30rpx;
		border-top: 1rpx solid #eee;
	}

	.filter-btn {
		flex: 1;
		height: 80rpx;
		border: none;
		border-radius: 12rpx;
		font-size: 28rpx;

		&.secondary {
			background-color: #f8f9fa;
			color: #666;
		}

		&.primary {
			background-color: #007bff;
			color: white;
		}
	}

	.filter-btn::after {
		border: none;
	}

	.load-more {
		padding: 30rpx;
		text-align: center;
	}

	.load-more-text {
		font-size: 26rpx;
		color: #666;
	}
</style>